package com.ruoyi.web.controller.minglvmini;


import com.github.pagehelper.Page;
import com.minglv.mini.domain.dto.NMsgDto;
import com.minglv.mini.domain.dto.NeedDto;
import com.minglv.mini.service.INeedService;
import com.minglv.mini.tools.MiniResult;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "API-需求")
@RestController
@RequestMapping("/api/need")
public class ApiNeedController {

    @Resource
    INeedService needService;

    @GetMapping("/list")
    @ApiOperation("需求列表")
    public Object getList(@RequestParam(name = "page")@ApiParam(value="页数") Integer page,
                          @RequestParam(name = "status",required = false) Integer status,
                          @RequestParam(name = "limit",required = false) Integer limit){
        Page<Object> objects = PageUtils.startPage(page,ObjectUtils.isEmpty(limit)?10:limit);
        List<Map> needs = needService.getList(ObjectUtils.isEmpty(status)?1:status);
//        List<Map> needs = needService.getList(status);
        Map data = new HashMap();
        data.put("list",needs);
        data.put("count",objects.getTotal());
        return  MiniResult.success(data);
    }

    @PostMapping("/info")
    @ApiOperation("需求详情")
    public MiniResult getInfo(@RequestBody @ApiParam(value = "参数") Map param) {

        //  @RequestParam(name = "id")@ApiParam(value="id") Long id,@RequestParam(name = "uid",required = false)@ApiParam(value="user_id") Long uid
        Long id = Long.valueOf(ObjectUtils.isEmpty(param.get("id")) ? "0" : param.get("id").toString());
        Long uid = Long.valueOf(ObjectUtils.isEmpty(param.get("uid")) ? "0" : param.get("uid").toString());
        Map info = needService.getInfo(id, uid);
        return MiniResult.success(info);
    }

    @PostMapping("/save")
    @ApiOperation("发布需求")
    public Object postSave(HttpServletRequest request, @RequestBody NeedDto needDto){
        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token)) {
            String[] s = token.split(" ");
            if (!ObjectUtils.isEmpty(s) && s.length > 0) {
                token = s[1];
                Object o = needService.postSave(token, needDto);
                return  o;
            }else{
                Map reMap = new HashMap();
                reMap.put("code",401);
                reMap.put("msg","Token过期,或无此用户");
                return reMap;
            }
        }else{
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }
    }

    @PostMapping("/saveMsg")
    @ApiOperation("保存留言")
    public Object postSaveMsg(HttpServletRequest request, @RequestBody NMsgDto nMsgDto){
        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token)) {
            String[] s = token.split(" ");
            if (!ObjectUtils.isEmpty(s) && s.length > 0) {
                token = s[1];
                return needService.postSaveMsg(token,nMsgDto);
            }else{
                Map reMap = new HashMap();
                reMap.put("code",401);
                reMap.put("msg","Token过期,或无此用户");
                return reMap;
            }
        }else{
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }
    }
}
